package com.fancy.learncenter.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.CartoonErrorUnitListItemBean;
import com.fancy.learncenter.bean.CartoonListByClassItemBean;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.ui.activity.PreviewHomeWorkActivity;
import com.fancy.learncenter.ui.activity.TeacherPreviewHomeworkActivity;
import com.fancy.learncenter.ui.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.ui.adapter.base.CustomViewHold;
import com.fancy.learncenter.ui.callback.AlbumImpl;

import java.util.List;

/**
 * Created by shiweiwei on 2018/1/24.
 */

public class CartoonAssginHomeWorkAdapter extends CommonRecycleViewAdapter<CartoonListByClassItemBean> {

    private AlbumImpl listener;
    private String classId;

    public void setListener(AlbumImpl listener) {
        this.listener = listener;
    }

    List<CartoonListByClassItemBean> itemDatas;

    public CartoonAssginHomeWorkAdapter(Context mContext, List<CartoonListByClassItemBean> itemDatas, String classId) {
        super(mContext, R.layout.cartoon_assign_homework_item_layout, itemDatas);
        this.classId = classId;
        this.itemDatas = itemDatas;
    }

    @Override
    public void bindView(CustomViewHold customViewHold, final CartoonListByClassItemBean item, final int position) {

        TextView unit_name_text = customViewHold.getView(R.id.unit_name_text);

        ImageView choose_image = customViewHold.getView(R.id.choose_image);

        TextView time_duration_text = customViewHold.getView(R.id.time_duration_text);

        if (!TextUtils.isEmpty(item.getUnitName())) {
            unit_name_text.setText(item.getUnitName());
        }

        if ("1".equals(item.getIsLock())) {
            customViewHold.getRoorView().setEnabled(false);
        } else {
            customViewHold.getRoorView().setEnabled(true);
        }

        LinearLayout rootView = (LinearLayout) customViewHold.getRoorView();

        if ("1".equals(item.getIsLock())) {
            rootView.setBackgroundColor(Color.parseColor("#CCCCCC"));

            choose_image.setImageResource(R.mipmap.cartoon_assign_homework_not_choose);

        } else {
            rootView.setBackgroundColor(Color.parseColor("#FFFFFF"));

            if (!item.isChecked()) {
                choose_image.setImageResource(R.mipmap.cartoon_assign_homework_not_choose);
            } else {
                choose_image.setImageResource(R.mipmap.cartoon_assign_homework_choose);
            }
        }


        choose_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                for (int i = 0; i < itemDatas.size(); i++) {

                    if (position != i) {
                        itemDatas.get(i).setChecked(false);
                    }

                }

                if (item.isChecked()) {
                    item.setChecked(false);
                } else {
                    item.setChecked(true);
                }

                notifyDataSetChanged();

                if (item.isChecked() && listener != null) {
                    listener.onClick(position);
                }
            }
        });



        time_duration_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext, PreviewHomeWorkActivity.class);
                intent.putExtra(JumpIntentKey.HOMEWORK_PACKAGE_ID, item.getHomeworkPackageId());
                intent.putExtra(JumpIntentKey.UNIT_ID, item.getUnitId());
                intent.putExtra(JumpIntentKey.CLASSID, classId);
//                Intent intent = new Intent(mContext, TeacherPreviewHomeworkActivity.class);
//                intent.putExtra(JumpIntentKey.HOMEWORK_PACKAGE_ID,item.getHomeworkPackageId());
                mContext.startActivity(intent);
            }
        });
    }


}
